import simpleGit, { SimpleGitOptions } from "simple-git";
import chalk from "chalk";
import createLogger from "progress-estimator";
import { log } from "../utils/log";

// 初始化进度条
const loger = createLogger({
  spinner: {
    interval: 100,
    frames: ["-", "+", "-"].map((item) => chalk.green(item)),
  },
});

const gitOptions: Partial<SimpleGitOptions> = {
  baseDir: process.cwd(), // 当前工作目录
  binary: "git", // git 二进制文件路径
  maxConcurrentProcesses: 6, // 最大并发进程数
  trimmed: false, // 是否去除空格
};
export const clone = async (
  url: string,
  projectName: string,
  options?: string[]
) => {
  const git = simpleGit(gitOptions);
  try {
    // 利用 progress-estimator 插件来显示进度条
    await loger(git.clone(url, projectName, options), "代码下载中...", {
      estimate: 7000, // 预计需要7秒
    });
    log.success(chalk.green(`${projectName}代码下载成功`));
    log.info(chalk.blackBright("================================"));
    log.info(chalk.blackBright("=======欢迎使用hcreate-cli脚手架======="));
    log.info(chalk.blackBright("================================"));
    log.info('');
    log.info('');
    log.info(chalk.blackBright("===请使用 pnpm install 安装依赖==="));
    log.info(chalk.blackBright("===== pnpm run dev 运行项目 ====="));
  } catch (error) {
    log.error(JSON.stringify(error));
    log.success(chalk.red(`${projectName}代码下载失败`));
  }
};
